package doc2db.interfaces.action.save;

import doc2db.Keey;
import doc2db.fileWorker.FileIO;
import doc2db.fileWorker.FileWorker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.JTextComponent;

/**
 *
 * @author yura
 */
public class SaveAction implements ActionListener{
    private JTextComponent text ;

    public SaveAction(JTextComponent text) {
        this.text = text;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        //chooser.setApproveButtonText("сохранить");
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        chooser.setDialogTitle("doc2db:Сохранить");
        try {
            chooser.setCurrentDirectory(new File(Keey.classPath));
        } catch (Exception ex) {
            chooser.setCurrentDirectory(new File("."));
        }
        chooser.setFileFilter(new FileNameExtensionFilter("Html document",
                new String[]{"html", "htm", "HTML", "HTM"}));

        int r = chooser.showOpenDialog(null);

        if (r == JFileChooser.APPROVE_OPTION) {
            String fname = chooser.getSelectedFile().getAbsolutePath();
            if (!(fname.endsWith(".html") | fname.endsWith(".htm") |
                    fname.endsWith(".HTML") | fname.endsWith(".HTM"))) {
                fname = fname + ".html";
            }
            FileIO.createFile(text.getText(), fname);
        }
    }

}
